<template>
  <div class="content-page">
    <div class="header flex justify-between items-center mb-8">
      <h1 class="text-2xl font-semibold">实习信息页面</h1>
      <el-button type="primary" @click="showDialog = true">上传认证</el-button>
    </div>

    <div class="certificate-gallery">
      <div class="certificate-container" v-for="(certificate, index) in certificates" :key="index">
        <h3>{{ certificate.name }}</h3>
        <img :src="certificate.image" :alt="`Certificate Image ${index + 1}`" style="width: 200px;height:200px">
        <div class="certificate-info">
          <p><strong>公司规模:</strong> {{ certificate.type }}</p>
          <p><strong>实习岗位:</strong> {{ certificate.level }}</p>
          <p><strong>开始时间:</strong> {{ certificate.startTime }}</p>
          <p><strong>结束时间:</strong> {{ certificate.endTime }}</p>
          <p><strong>公司:</strong> {{ certificate.organizer }}</p>
          <p>{{ certificate.description }}</p>
        </div>
        <button class="delete-button" @click="confirmRemoveCertificate(index)">删除</button>
      </div>
    </div>

    <!-- 弹窗 -->
    <div v-if="showDialog" class="dialog-overlay fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center">
      <div class="dialog-content bg-white rounded-tl-lg rounded-bl-lg w-[80vw] max-w-[800px] p-8">
        <div class="dialog-header mb-6">
          <h2 class="text-xl font-medium">提交实习信息</h2>
        </div>
        
        <div class="dialog-body">
          <form @submit.prevent="handleSubmit">
            <!-- 表单内容保持不变 -->
            <div class="form-item mb-4">
              <label class="required-label block mb-2">公司名称:</label>
              <el-input 
                v-model="formData.name"
                placeholder="请输入实习公司名称"
                class="w-full text-sm"
              />
            </div>

            <div class="form-item mb-4">
              <label class="required-label block mb-2">公司规模:</label>
              <el-select 
                v-model="formData.type"
                placeholder="请选择公司规模"
                class="w-full text-sm"
              >
                <el-option label="科技创新" value="tech" />
                <el-option label="学科竞赛" value="subject" />
                <el-option label="创业竞赛" value="startup" />
              </el-select>
            </div>

            <div class="form-item mb-4">
              <label class="required-label block mb-2">实习岗位:</label>
              <el-select
                v-model="formData.level" 
                placeholder="请选择实习岗位"
                class="w-full text-sm"
              >
                <el-option label="国家级" value="national" />
                <el-option label="省级" value="provincial" />
                <el-option label="市级" value="city" />
              </el-select>
            </div>

            <div class="form-item mb-4">
              <label class="required-label block mb-2">实习时间:</label>
              <div class="flex items-center gap-4">
                <el-date-picker
                  v-model="formData.startTime"
                  type="date"
                  placeholder="选择开始时间"
                  class="flex-1 text-sm"
                />
                <span class="text-gray-400">-</span>
                <el-date-picker
                  v-model="formData.endTime"
                  type="date"
                  placeholder="选择结束时间"
                  class="flex-1 text-sm"
                />
              </div>
            </div>

            <div class="form-item mb-4">
              <label class="required-label block mb-2">组织单位:</label>
              <el-input
                v-model="formData.organizer"
                placeholder="请输入组织名称"
                class="w-full text-sm"
              />
            </div>

            <div class="form-item mb-4">
              <label class="required-label block mb-2">实习成果描述:</label>
              <el-input
                v-model="formData.description"
                type="textarea"
                :maxlength="50"
                :rows="4"
                placeholder="请输入实习成果证明"
                show-word-limit
                class="w-full text-sm"
              />
            </div>

            <div class="form-item mb-6">
              <label class="required-label block mb-2">图片证明材料:</label>
              <div 
                class="upload-area border-2 border-dashed border-gray-200 rounded-lg p-6 text-center cursor-pointer hover:border-blue-400 transition-colors"
                @click="triggerUpload"
              >
                <el-icon class="text-gray-400 text-2xl mb-2"><Plus /></el-icon>
                <input 
                  ref="fileInput"
                  type="file"
                  class="hidden"
                  accept="image/*"
                  @change="handleFileChange"
                >
                <div class="text-gray-500 text-sm">点击或拖拽上传图片</div>
                <div class="text-gray-400 text-xs mt-1">图片尺寸规则:A4纸为准,大小不大于1M</div>
              </div>
            </div>

            <div class="dialog-footer flex justify-end gap-4">
              <el-button @click="handleCancel" class="!rounded-button whitespace-nowrap">返回</el-button>
              <el-button 
                type="primary" 
                @click="handleSubmit"
                :disabled="!isFormValid"
                class="!rounded-button whitespace-nowrap"
              >
                提交
              </el-button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>


<script setup>
import { ref, computed } from 'vue';
import { Plus } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';

const showDialog = ref(false);
const fileInput = ref(null);

// interface FormData {
//   name: string;
//   type: string;
//   level: string;
//   startTime: string;
//   endTime: string;
//   organizer: string;
//   description: string;
//   file: File | null;
// }

const formData = ref<FormData>({
  name: '',
  type: '',
  level: '',
  startTime: '',
  endTime: '',
  organizer: '',
  description: '',
  file: null
});

const isFormValid = computed(() => {
  return formData.value.name && 
         formData.value.type && 
         formData.value.level && 
         formData.value.startTime && 
         formData.value.endTime &&
         formData.value.organizer &&
         formData.value.description;
});

const triggerUpload = () => {
  fileInput.value?.click();
};

const handleFileChange = (event) => {
  const target = event.target ;
  if (target.files && target.files[0]) {
    const file = target.files[0];
    if (file.size > 1024 * 1024) {
      ElMessage.error('文件大小不能超过1M');
      return;
    }
    formData.value.file = file;
  }
};

const handleSubmit = () => {
  if (!isFormValid.value) {
    ElMessage.warning('请填写完整所有必填项');
    return;
  }
  ElMessage.success('提交成功');
  showDialog.value = false; // 提交成功后关闭弹窗
};

const handleCancel = () => {
  showDialog.value = false;
  ElMessage.info('已取消');
};

const certificates = ref([
  {
    name: "北京科技有限公司",
    type: "大型企业",
    level: "软件开发工程师",
    startTime: "2023-06-01",
    endTime: "2023-08-31",
    organizer: "ABC公司",
    description: "完成了一个项目管理系统的开发",
    image: "https://ai-public.mastergo.com/ai/img_res/139ab9e3299a30b1bcaf87c75ea8d69e.jpg"
  },
  {
    name: "上海金融集团",
    type: "中型企业",
    level: "数据分析师",
    startTime: "2023-07-01",
    endTime: "2023-09-30",
    organizer: "XYZ公司",
    description: "分析了客户数据并提出优化建议",
    image: "https://ai-public.mastergo.com/ai/img_res/8a2d123aef8db4d2e017a1c174edd401.jpg"
  },
  {
    name: "广州广告公司",
    type: "小型企业",
    level: "市场营销专员",
    startTime: "2023-05-01",
    endTime: "2023-07-31",
    organizer: "广告公司",
    description: "策划并执行了三次市场推广活动",
    image: "https://ai-public.mastergo.com/ai/img_res/75979d8b6a3d45c38eb8e6124d3e438e.jpg"
  },
  {
    name: "深圳电子科技公司",
    type: "大型企业",
    level: "硬件工程师",
    startTime: "2023-06-15",
    endTime: "2023-09-15",
    organizer: "电子公司",
    description: "参与了新产品的硬件设计和测试",
    image: "https://ai-public.mastergo.com/ai/img_res/c8e123567c3b4e2a8c7a1c2a4f963ae9.jpg"
  },
  {
    name: "成都教育培训机构",
    type: "中型企业",
    level: "教学助理",
    startTime: "2023-04-01",
    endTime: "2023-06-30",
    organizer: "教育公司",
    description: "协助老师完成教学任务并辅导学生",
    image: "https://ai-public.mastergo.com/ai/img_res/dfcb2b123456789a1bcdfge12345.jpg"
  },
  {
    name: "杭州互联网公司",
    type: "大型企业",
    level: "前端开发工程师",
    startTime: "2023-05-15",
    endTime: "2023-08-15",
    organizer: "互联网公司",
    description: "开发了公司官网的前端页面",
    image: "https://ai-public.mastergo.com/ai/img_res/789cdef4567b345c12abcdf56789.jpg"
  }
]);

function confirmRemoveCertificate(index) {
  ElMessageBox.confirm(
    '您确定要删除这个实习记录吗？此操作不可逆！',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    removeCertificate(index);
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消删除'
    });
  });
}

function removeCertificate(index) {
  certificates.value.splice(index, 1);
}
</script>


<style scoped>
.required-label::before {
  content: '*';
  color: #f56c6c;
  margin-right: 4px;
}

.dialog-overlay {
  z-index: 1000;
}

.dialog-content {
  width: 90vw;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
  background: #fff;
  border-radius: 1rem 0 0 1rem; /* 仅左上角和左下角是圆角 */
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

/* 自定义滚动条样式 */
.dialog-content::-webkit-scrollbar {
  width: 8px; /* 滚动条宽度 */
}

.dialog-content::-webkit-scrollbar-track {
  background: #f1f1f1; /* 滚动条轨道背景 */
  border-radius: 1rem; /* 确保轨道有圆角 */
}

.dialog-content::-webkit-scrollbar-thumb {
  background: #888; /* 滚动条颜色 */
  border-radius: 1rem; /* 确保滚动条有圆角 */
}

.dialog-content::-webkit-scrollbar-thumb:hover {
  background: #555; /* 悬停时滚动条颜色 */
}

.certificate-gallery {
  display: grid;
  grid-template-columns: repeat(2, 1fr); /* 每行显示两个证书 */
  gap: 2rem; /* 证书之间的间距 */
  justify-items: center; /* 证书居中对齐 */
}

.certificate-container {
  position: relative; /* 使删除按钮能够定位在证书盒子内 */
  border-radius: 1rem;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  padding: 1rem;
  text-align: center;
  width: 400px;
  max-width: 400px; /* 设置证书盒子的最大宽度 */
}

h3 {
  font-size: 1.25rem;
  color: #333;
}

img {
  display: block;
  margin: 0.5rem auto; /* 图片居中 */
  border-radius: 0.5rem;
}

.certificate-info {
  text-align: left; /* 信息左对齐 */
  margin-top: 1rem;
}

p {
  font-size: 1rem;
  color: #00796b;
  margin: 0.5rem 0;
}

.delete-button {
  position: absolute; /* 定位在证书盒子的右下角 */
  bottom: 1rem;
  right: 1rem;
  background-color: red;
  color: white;
  border: none;
  padding: 0.3rem 0.5rem; /* 按钮尺寸更小 */
  border-radius: 0.5rem;
  cursor: pointer;
}

.delete-button:hover {
  background-color: darkred; /* 鼠标悬停时改变按钮颜色 */
}
</style>
